﻿using AutoMapper;
using B.S.Ingredients.APIRead.Application.Command.Products;
using B.S.Ingredients.APIRead.DTOs;
using B.S.Ingredients.Domain;
using B.S.Ingredients.ErrorCode;
using B.S.Ingredients.Infartructrue.Interfaces;
using MediatR;

namespace B.S.Ingredients.APIRead.Application.CommandHandler.ProductHandler
{
    public class ProdictFtCommandHandler : IRequestHandler<ProdictFtCommand, APIResult<ProductDTO>>
    {
        public ProdictFtCommandHandler(IBaseRepository<Product> productRepository,IBaseRepository<ProduciNameClass> nameClassRepository,IMapper mapper)
        {
            ProductRepository = productRepository;
            NameClassRepository = nameClassRepository;
            Mapper = mapper;
        }

        public IBaseRepository<Product> ProductRepository { get; }
        public IBaseRepository<ProduciNameClass> NameClassRepository { get; }
        public IMapper Mapper { get; }

        public async Task<APIResult<ProductDTO>> Handle(ProdictFtCommand request, CancellationToken cancellationToken)
        {
            var path = ProductRepository.GetAll().FirstOrDefault(a => a.Productuin == request.productuin);
            var listDto = Mapper.Map<ProductDTO>(path);
            listDto.ProdName= NameClassRepository.GetAll().FirstOrDefault(a => a.ProdId==listDto.ProdId)?.ProdName;
            return new APIResult<ProductDTO>
            {
                Code = ResultCode.success,
                Mag = "",
                Data = listDto
            };
        }
    }
}
